package cn.scut.comment.controller;

import cn.scut.comment.utils.Model;
import com.alibaba.fastjson.JSONObject;
import cn.scut.comment.pojo.Comment;
import cn.scut.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    CommentService commentService;

    @GetMapping("/getComment")
    public Model apiGetComments(@RequestBody JSONObject jsonObject) {
        String postid = jsonObject.getString("postid");
        List<Comment> comments = commentService.listPostComments(postid);
        if (comments != null) {
            return new Model(true, comments, "该帖子回复信息如下");
        } else {
            return new Model(false, null, "该帖子还没有回复或发生了意外的错误");
        }
    }

    @PutMapping("/setComment")
    public Model apiSetComment(@RequestBody Comment comment) {
        boolean save = commentService.save(comment);
        if (save) {
            return new Model(true, true, "回复成功");
        } else {
            return new Model(false, false, "回复失败");
        }
    }

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }
}
